﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Orders;
using LuxDiner.Command.Orders;
using LuxDiner.Logic.Financial;
using Utils;
namespace LuxDiner.Logic.Orders
{
    public class PaidOrderLogic
    {
        public static PaidOrderModel NewOrder(PaidOrderModel order)
        {
            if (order != null)
            {
                if (order.Items == null || order.Items.Count == 0)
                    throw new LuxDinerException("没有内容");
                if (order.CustomerID == 0)
                    throw new LuxDinerException("请选择一个客户");
                var customer = Members.CustomerLogic.GetModel(order.CustomerID);
                if (customer == null)
                    throw new LuxDinerException("无效的客户");

                order.CreatedOn = DateTime.Now;
                order.TotalMoney = order.Items.Sum(p => p.Money);
                if (string.IsNullOrEmpty(order.Description))
                    order.Description = (order.Method == (int)Model.Orders.OrderMethod.FK) ? "付款单" : "收款单";
                foreach (var it in order.Items)
                {
                    if (it.Money == 0)
                        throw new ApplicationException("无效的金额");

                    var bank = BankLogic.GetModel(it.BankID);
                    if (bank == null)
                        throw new ApplicationException("无效的银行帐号");
                    it.Bank = bank;
                    order.CreatedOn = DateTime.Now;
                }
                return PaidOrderCommand.NewOrder(order);
            }
            return null;
        }

        public static PaidOrderCollection GetOrders(PaidOrderQuery sea)
        {
            return PaidOrderCommand.GetOrders(sea);
        }

        public static PaidOrderModel GetOrder(string orderNo)
        {
            return PaidOrderCommand.GetOrder(orderNo);
        }
    }
}
